package com.huiquan.icd.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.BaseService;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.GetListUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.dao.IcdDiseaseArticleDao;
import com.huiquan.icd.domain.IcdDiseaseArticle;

@Service
public class IcdDiseaseArticleService extends BaseService {

	
	@Autowired
	private IcdDiseaseArticleDao icdDiseaseArticleDao;
	public ModelAndView list(Integer start, String codeSearch,
			String exactSearchFlag,String countPerPageStr,String diseaseSearch) {
		boolean exactFlag = exactSearchFlag != null && exactSearchFlag.equals("1");

		Map<String, Object> param = new HashMap<>();
		if (codeSearch != null && !codeSearch.isEmpty()) {
			param.put("title", exactFlag ? codeSearch : "%" + codeSearch + "%");
		}
		if (diseaseSearch != null && !diseaseSearch.isEmpty()) {
			param.put("disease", exactFlag ? diseaseSearch : "%" + diseaseSearch + "%");
		}
		int totalSize = icdDiseaseArticleDao.retrieveSize(param);

		Map<String, Integer> pageNo = new HashMap<>();
		if (countPerPageStr != null && !countPerPageStr.isEmpty()) {
			int countPerPage = Integer.parseInt(countPerPageStr);
			pageNo = GetListUtil.getPageNoParam(totalSize, start+"", countPerPage);
			param.put("countPerPage", countPerPage);
		} else {
			pageNo = GetListUtil.getPageNoParam(totalSize, start+"");
		}
		int endPage = pageNo.get("endPage");
		start = pageNo.get("start");
		int startIndex = pageNo.get("startIndex");

		List<IcdDiseaseArticle> list = new ArrayList<>();

		if (startIndex < totalSize) {
			param.put("startIndex", startIndex);
			list = icdDiseaseArticleDao.retrievesList(param);
		}
		Map<String, String> showSearch = new LinkedHashMap<>();
		showSearch.put("codeSearch", "题目");
		showSearch.put("diseaseSearch", "疾病");
		Map<String, String> searchMap = new HashMap<>();
		searchMap.put("codeSearch", codeSearch);
		Map<String, Object> map = GetListUtil.getReturnMap2(totalSize, start, startIndex, endPage, list, searchMap);
		map.put("showSearch", showSearch);
		map.put("diseaseSearch", diseaseSearch);
		map.put("showSearchValue", searchMap);
		map.put("exactSearchFlag", exactSearchFlag);
		return new ModelAndView("icd/index", map);
	}
	public ModelAndView preEdit(String codeSearch, String nameSearch, String categorySearch, Integer start, String id,
			User user, String exactSearchFlag) {
		// 如果id不为空则表示是修改，否则为新增
		Map<String, Object> map = new HashMap<>();
		if (id != null && !id.isEmpty()) {
			IcdDiseaseArticle article = icdDiseaseArticleDao.retrieveObjectById(Long.parseLong(id));
			map.put("title", article.getTitle());
			map.put("content", article.getContent());
			map.put("url", article.getUrl());
			map.put("disease", article.getDisease());
			map.put("type", article.getType());
			map.put("source", article.getSource());

		}
		map.put("id", id);
		map.put("start", start);
		map.put("codeSearch", codeSearch);
		map.put("nameSearch", nameSearch);
		map.put("categorySearch", categorySearch);
		map.put("exactSearchFlag", exactSearchFlag);
		return new ModelAndView("icd/disease_article_edit", map);
	}


	public ReturnData updateAndAddDiseaseArticl(String id,String title,String disease,String url,String type,String source,String content,User user) {
		//修改操作
		if(StringUtils.isNotBlank(id)){
			IcdDiseaseArticle article =  icdDiseaseArticleDao.retrieveObjectById(Long.parseLong(id));
			article = getDiseaseArticl(title, disease, url, type, source, content, article);
			icdDiseaseArticleDao.update(article);
		}else{
			//新增操作
			IcdDiseaseArticle article = new IcdDiseaseArticle();
			article = getDiseaseArticl(title, disease, url, type, source, content, article);
			icdDiseaseArticleDao.insert(article);
		}
		return ReturnUtil.success();
	}
	private IcdDiseaseArticle getDiseaseArticl(String title,String disease,String url,String type,String source,String content,IcdDiseaseArticle article){
		 article.setContent(content);
		 article.setDelFlag("0");
		 article.setDisease(disease);
		 article.setTitle(title);
		 article.setUrl(url);
		 article.setSource(source);
		 article.setType(type);
		 return article;
	}
	public ReturnData deleteArticle(Long id) {
		//查询疾病维表是否有该疾病
		icdDiseaseArticleDao.deleteArticle(id);
	return ReturnUtil.success();
}
	
}
